<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="core.*,util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="/WEB-INF/include/checkAdmin.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    div.form-inline { margin-bottom: 5px; }
    select[name=sz] { width: 60px; }
    select[name=od] { width: 100px; }
    select[name=ss] { width: 100px; margin-left: 100px; }
    table.table th:first-child { width: 20px; }
    tbody tr:hover { background-color: #FFC; cursor: pointer; }
</style>
<script>
$(function() {
    $("select[name=od], select[name=sz]").change(function() {
        $("form").submit();
    })
    $("thead input[type=checkbox]").click(function() {
        $("tbody input[type=checkbox]").trigger("click");
    })
    $("tbody tr").click(function() {
        location.href = "log.jsp?id=" + $(this).attr("data-id") + location.search.replace("?", "&"); 
    })
    $("tbody td:nth-child(1)").click(function() {
        event.stopPropagation();
    })
    $("div.pagination a").click(function() {
        $("input[name=pg]").val($(this).attr("data-page"));
        $("form").submit();
    })
})
</script>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int currentPage = param.getInt("pg", 1);
    int pageSize = param.getInt("sz", 15);
    int order = param.getInt("od", 0);
    int srchType = param.getInt("ss", 0);
    String srchText = param.getString("st", "");
    String cmd = param.getString("cmd", "");
    String errMsg = null;

    if (cmd.equals("delete")) {
        int[] idList = param.getIntValues("did");
        LogDAO.delete(idList);
        String qs = request.getQueryString().replaceAll("did=[0-9]+&?","").replaceAll("cmd=delete&?","");
        response.sendRedirect("log.jsp?" + qs);
        return;
    }
    ArrayList<Log> list = LogDAO.selectPage(currentPage, pageSize, order, srchType, srchText);

    int recordCount = LogDAO.getRecordCount(srchType, srchText);
    int pageCount = (recordCount + pageSize - 1 ) / pageSize;
    int basePage = ((currentPage - 1) / 10) * 10;
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container main">

    <h1>로그기록</h1>
    <hr />

    <form>
        <input type="hidden" name="pg" value="1" />

        <div class="pull-right">
            <button type="submit" class="btn btn-small" name="cmd" value="delete">삭제</button>
        </div>

        <div class="form-inline">
            정렬:
            <select name="od">
                <option value="0" <%= order==0 ? "selected" : "" %>>시각</option>
                <option value="1" <%= order==1 ? "selected" : "" %>>사용자</option>
                <option value="2" <%= order==2 ? "selected" : "" %>>IP</option>
                <option value="3" <%= order==3 ? "selected" : "" %>>구분</option>
            </select>

            <select name="ss">
                <option value="0" <%= srchType==0 ? "selected" : "" %>>검색없음</option>
                <option value="1" <%= srchType==1 ? "selected" : "" %>>사용자</option>
                <option value="2" <%= srchType==2 ? "selected" : "" %>>URL</option>
                <option value="3" <%= srchType==3 ? "selected" : "" %>>구분</option>
                <option value="4" <%= srchType==4 ? "selected" : "" %>>내용</option>
            </select>

            <input type="text" name="st" value="<%= srchText %>" />
            <button type="submit" class="btn btn-small search">검색</button>
        </div>

        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <th><input type="checkbox" /></th>
                    <th>사용자</th>
                    <th>IP</th>
                    <th>구분</th>
                    <th>일시</th>
                </tr>
            </thead>
            <tbody>
                <% for (Log log : list) { %>
                    <tr data-id="<%=log.getId()%>">
                        <td><input type="checkbox" name="did" value="<%=log.getId()%>"></td>
                        <td><%= log.getName() %></td>
                        <td><%= log.getIP() %></td>
                        <td><%= log.getCategory() %></td>
                        <td><%= DateUtil.toYMDHMS(log.getWriteTime()) %></td>
                    </tr>
                <% } %>
            </tbody>
        </table>

        <div class="controls pull-right form-inline">
            화면크기:
            <select name="sz">
                <option <%= pageSize==10 ? "selected" : "" %>>10</option>
                <option <%= pageSize==15 ? "selected" : "" %>>15</option>
                <option <%= pageSize==30 ? "selected" : "" %>>30</option>
                <option <%= pageSize==60 ? "selected" : "" %>>60</option>
                <option <%= pageSize==100 ? "selected" : "" %>>100</option>
            </select>
        </div>

        <div class="pagination pagination-small pagination-centered">
            <ul>
                <% if (basePage > 0) { %>
                    <li><a data-page="<%= basePage %>">Prev</a></li>
                <% } %>
                <% for (int i=1; i <= 10 && basePage + i <= pageCount; ++i) { %>
                    <li class='<%= basePage+i==currentPage ? "active" : "" %>'>
                        <a data-page="<%= basePage+i %>"><%= basePage + i %></a>
                    </li>
                <% } %>
                <% if (basePage + 11 <= pageCount) { %>
                    <li><a data-page="<%= basePage+11 %>">Next</a></li>
                <% } %>
            </ul>
        </div>

    </form>

</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>
